package sanmubird.club.mock.dao.orm.embedded;

import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;
import sanmubird.club.mock.dao.beans.UserConverter;
import sanmubird.club.mock.dao.beans.UserDTO;
import sanmubird.club.mock.dao.beans.UserPo;
import sanmubird.club.mock.dao.orm.UserRepository;

import java.util.List;

/**
 * 使用这种方式 把整个项目都启动起来 可以 正常运行 但带来的问题是 需要把整个项目都启动起来,有些重了.
 * <p>
 * 注解 @ContextConfiguration 会实例化括号里面的classes; 从这里也应看到整个Spring并没有启动
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest4SpringBoot {

	@Autowired
	private UserConverter userConverter;

	@Autowired
	private UserRepository userRepository;

	@Rollback
	@Test()
	public void testSave() {
		UserDTO userDTO = UserDTO.builder().id(34).name("demo").age(12).build();
		UserPo userPo = userConverter.dtoToPo(userDTO);
		int save = userRepository.save(userPo);
		Assert.assertEquals(1, save);
		List<UserPo> userPos = userRepository.selectList(userPo);
		log.info("获取到的内容是: {}", userPos);
		Assert.assertFalse(CollectionUtils.isEmpty(userPos));
	}

	@Test
	public void testQuery() {
		UserDTO userDTO = new UserDTO();
		userDTO.setName("zs");
		UserPo userPo = userConverter.dtoToPo(userDTO);
		List<UserPo> userPos = userRepository.selectList(userPo);
		log.info("获取到的内容是: {}", userPos);
		Assert.assertFalse(CollectionUtils.isEmpty(userPos));
	}

	@Rollback
	@Test()
	public void testDelete() {
		UserDTO userDTO = new UserDTO();
		userDTO.setName("zs");
		UserPo userPo = userConverter.dtoToPo(userDTO);
		int delete = userRepository.delete(userPo);
		Assert.assertEquals(1, delete);
		List<UserPo> userPos = userRepository.selectList(userPo);
		log.info("获取到的内容是: {}", userPos);
		Assert.assertTrue(CollectionUtils.isEmpty(userPos));
	}
}